package com.example.springbootelasticsearch.controller;

import com.example.springbootelasticsearch.pojo.User;
import com.sun.security.jgss.InquireType;
import org.apache.lucene.search.BooleanQuery;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TestController
 * @Description TODO
 * @Author wangzhendong
 * @Data 22:05
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
public class TestController {
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    /*
    根据first_name查询
     */
    @RequestMapping("/search")
    @ResponseBody
    public List<User> search(){
        QueryBuilder builder = QueryBuilders.existsQuery("first_name");
        SearchQuery searchQuery= new NativeSearchQueryBuilder().withQuery(builder)
                                    .build();
        List<User> users = elasticsearchTemplate.queryForList(searchQuery, User.class);
        return users;
    }
    /*
    添加数据
     */
    @RequestMapping("/addList")
    @ResponseBody
    public String save(){
        List<IndexQuery> queries=new ArrayList<>();
//        User user4=new User(1L,"sun","yi",5,"I am 孙一");
//        User user5=new User(2L,"jia","er",15,"I am 嘉尔");
//        User user=new User(3L,"zhang","san",25,"I am 张四");
//        User user1=new User(4L,"wang","wu",35,"I am 王五");
//        User user2=new User(5L,"zhao","liu",45,"I am 赵柳");
//        User user3=new User(6L,"qian","qi",55,"I am 前期");
//        IndexQuery indexQuery=new IndexQueryBuilder()
//                        .withId(user.getId().toString())
//                        .withObject(user)
//                        .build();
//        IndexQuery indexQuery4=new IndexQueryBuilder()
//                .withId(user4.getId().toString())
//                .withObject(user4)
//                .build();
//        IndexQuery indexQuery5=new IndexQueryBuilder()
//                .withId(user5.getId().toString())
//                .withObject(user5)
//                .build();
//        IndexQuery indexQuery2=new IndexQueryBuilder()
//                .withId(user2.getId().toString())
//                .withObject(user2)
//                .build();
//        IndexQuery indexQuery3=new IndexQueryBuilder()
//                .withId(user3.getId().toString())
//                .withObject(user3)
//                .build();
//        IndexQuery indexQuery1=new IndexQueryBuilder()
//                .withId(user1.getId().toString())
//                .withObject(user1)
//                .build();
//        queries.add(indexQuery);
//        queries.add(indexQuery2);
//        queries.add(indexQuery3);
//        queries.add(indexQuery4);
//        queries.add(indexQuery5);
//        queries.add(indexQuery1);
        User user6=new User(7L,"qian","qi",65,"I am 前期");
        IndexQuery indexQuery1=new IndexQueryBuilder()
                .withId(user6.getId().toString())
                .withObject(user6)
                .build();
        queries.add(indexQuery1);
        //批量插入
        elasticsearchTemplate.bulkIndex(queries);
        return "success";
    }

    /**
     * 单字符进行查询
     * @return
     */
    @RequestMapping("searchOneString")
    @ResponseBody
    public List<User> searchOneString(){
        SearchQuery searchQuery=new NativeSearchQueryBuilder()
                                .withQuery(QueryBuilders.queryStringQuery("张"))
                                .withPageable(new PageRequest(0,20))
                                .build();
        List<User> users = elasticsearchTemplate.queryForList(searchQuery, User.class);
        return users;
    }

    /**
     * 根据年龄排序
     * @return
     */
    @RequestMapping("searchOrderByAge")
    @ResponseBody
    public Page<User> searchOrderByAge(){
        PageRequest pageRequest=new PageRequest(0,20,new Sort(Sort.Direction.DESC,"age"));
        SearchQuery searchQuery=new NativeSearchQueryBuilder()
                                .withPageable(pageRequest)
                                .build();
        Page<User> list=elasticsearchTemplate.queryForPage(searchQuery,User.class);
        return list;
    }

    /**
     * 删除数据
     * @return
     */
    @RequestMapping("del")
    @ResponseBody
    public String del(){
//        elasticsearchTemplate.delete(User.class,"1");
//        elasticsearchTemplate.delete(User.class,"2");
        elasticsearchTemplate.delete(User.class,"3");
        elasticsearchTemplate.delete(User.class,"3");
//        elasticsearchTemplate.delete(User.class,"4");
        return "del_success";
    }

    /**
     * 根据某一字段模糊查询,只要含有am就会被查询出来
     * @return
     */
    @RequestMapping("searchLike")
    @ResponseBody
    public List<User> searchLike(){
        Pageable pageable=new PageRequest(0,10);
        SearchQuery searchQuery=new NativeSearchQueryBuilder()
                                .withQuery(QueryBuilders.matchQuery("about","am"))
                                .withPageable(pageable)
                                .build();
        List<User> users = elasticsearchTemplate.queryForList(searchQuery, User.class);
        return users;
    }

    /**
     * 模糊查询about字段中带有前
     * @return
     */
    @RequestMapping("searchLike1")
    @ResponseBody
    public List<User> searchLike1(){
        Pageable pageable=new PageRequest(0,10);
        SearchQuery searchQuery=new NativeSearchQueryBuilder()
                                .withQuery(QueryBuilders.matchPhraseQuery("about","前"))
                                .withPageable(pageable)
                                .build();
        List<User> users = elasticsearchTemplate.queryForList(searchQuery, User.class);
        return users;
    }

    /**
     * 根据字段全部匹配
     * @return
     */
    @RequestMapping("searchTerm")
    @ResponseBody
    public List<User> searchTerm(){
        SearchQuery searchQuery=new NativeSearchQueryBuilder()
                                .withQuery(QueryBuilders.termQuery("age",55))
                                .build();
        List<User> users = elasticsearchTemplate.queryForList(searchQuery, User.class);
        return users;
    }

    /**
     * 指定查询的字段
     * @return
     */
    @RequestMapping("searchFetch")
    @ResponseBody
    public List<User> searchFetch(){
        String[] include={"first_name","age","about"};
        FetchSourceFilter fetchSourceFilter=new FetchSourceFilter(include,null);//两个参数，要显示的和不显示
        SearchQuery searchQuery=new NativeSearchQueryBuilder()
                                .withSourceFilter(fetchSourceFilter)
                                .build();
        return elasticsearchTemplate.queryForList(searchQuery,User.class);
    }

    /**
     * 高亮查询和模糊查询相似
     * @return
     */
    @RequestMapping("high")
    @ResponseBody
    public List<User> mustSearch(){
        SearchQuery searchQuery=new NativeSearchQueryBuilder()
                                .withQuery(QueryBuilders.matchPhraseQuery("about","前"))
                                .withHighlightFields(new HighlightBuilder.Field("about"))
                                .build();
        return elasticsearchTemplate.queryForList(searchQuery,User.class);
    }
}
